4 * GD-Rom access functions.
6 * Copyright (c) 2005 Nathan Keynes.
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.
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.
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"
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 )
37 CALL_HOOKS( gdrom_disc_change_hook, disc, disc == NULL ? NULL : disc->name );
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 )
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);
53 void gdrom_device_destroy( gdrom_device_t dev )
55 if( dev->name != NULL ) {
59 if( dev->device_name != NULL ) {
60 g_free( dev->device_name );
61 dev->device_name = NULL;
66 void gdrom_mount_disc( gdrom_disc_t disc )
68 if( disc != gdrom_disc ) {
69 if( gdrom_disc != NULL ) {
70 gdrom_disc->destroy(gdrom_disc,TRUE);
73 gdrom_disc_read_title( disc );
74 gdrom_fire_disc_changed( disc );
78 gboolean gdrom_mount_image( const gchar *filename )
80 gdrom_disc_t disc = gdrom_image_open(filename);
82 gdrom_mount_disc( disc );
88 void gdrom_unmount_disc( )
90 if( gdrom_disc != NULL ) {
91 gdrom_disc->destroy(gdrom_disc, TRUE);
92 gdrom_fire_disc_changed(NULL);
97 gdrom_disc_t gdrom_get_current_disc()
102 const gchar *gdrom_get_current_disc_name()
104 if( gdrom_disc == NULL ) {
107 return gdrom_disc->name;
111 const gchar *gdrom_get_current_disc_title()
113 if( gdrom_disc == NULL || gdrom_disc->title[0] == '\0' ) {
116 return gdrom_disc->title;
120 int gdrom_disc_get_track_by_lba( gdrom_disc_t disc, uint32_t lba )
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) ) {
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; } \
138 gdrom_error_t gdrom_disc_check_media( gdrom_disc_t disc )
144 gdrom_error_t gdrom_disc_get_toc( gdrom_disc_t disc, unsigned char *buf )
148 uint32_t first, last, leadout;
151 struct gdrom_toc *toc = (struct gdrom_toc *)buf;
156 for( i=0; i<disc->track_count; i++ ) {
157 toc->track[i] = htonl( disc->track[i].lba ) | disc->track[i].flags;
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;
164 toc->track[i] = 0xFFFFFFFF;
168 gdrom_error_t gdrom_disc_get_session_info( gdrom_disc_t disc, int session, unsigned char *buf )
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;
175 buf[0] = 0x01; /* Disc status? */
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;
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;
195 return PKT_ERR_BADFIELD; /* No such session */
199 gdrom_error_t gdrom_disc_get_short_status( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
203 int track_no = gdrom_disc_get_track_by_lba( disc, lba );
204 if( track_no == -1 ) {
208 struct gdrom_track *track = &disc->track[track_no-1];
209 uint32_t offset = lba - track->lba;
211 buf[1] = 0x15; /* audio status ? */
214 buf[4] = track->flags;
216 buf[6] = 0x01; /* ?? */
217 buf[7] = (offset >> 16) & 0xFF;
218 buf[8] = (offset >> 8) & 0xFF;
219 buf[9] = offset & 0xFF;
221 buf[11] = (lba >> 16) & 0xFF;
222 buf[12] = (lba >> 8) & 0xFF;
223 buf[13] = lba & 0xFF;
227 int gdrom_disc_get_drive_status( gdrom_disc_t disc )
230 return IDE_DISC_NONE;
233 disc->check_status(disc);
234 if( disc->disc_type == IDE_DISC_NONE ) {
235 return IDE_DISC_NONE;
237 return disc->disc_type | IDE_DISC_READY;
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.
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 ) {
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 ) {
263 memcpy( disc->title, boot_sector+128, 128 );
264 for( i=127; i>=0; i-- ) {
265 if( !isspace(disc->title[i]) )
268 disc->title[i+1] = '\0';
270 bootstrap_dump(boot_sector, FALSE);
.