Search
lxdream.org :: lxdream/src/gdrom/gdrom.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 342:850502f0e8de
prev245:a1d0655a88d3
next422:61a0598e07ff
author nkeynes
date Wed Jan 31 10:58:42 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Refactor gdrom module to be more conducive to real device support
file annotate diff log raw
1.1 --- a/src/gdrom/gdrom.c Tue Dec 19 09:52:56 2006 +0000
1.2 +++ b/src/gdrom/gdrom.c Wed Jan 31 10:58:42 2007 +0000
1.3 @@ -1,5 +1,6 @@
1.4 +
1.5 /**
1.6 - * $Id: gdrom.c,v 1.11 2006-12-19 09:52:56 nkeynes Exp $
1.7 + * $Id: gdrom.c,v 1.12 2007-01-31 10:58:42 nkeynes Exp $
1.8 *
1.9 * GD-Rom access functions.
1.10 *
1.11 @@ -24,13 +25,10 @@
1.12 #include "gdrom/packet.h"
1.13 #include "dream.h"
1.14
1.15 -static void gdrom_image_destroy( gdrom_disc_t );
1.16 -static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, int, char *, uint32_t * );
1.17 +extern gdrom_disc_t gdrom_disc;
1.18
1.19 gdrom_image_class_t gdrom_image_classes[] = { &linux_device_class, &nrg_image_class, &cdi_image_class, NULL };
1.20
1.21 -gdrom_disc_t gdrom_disc = NULL;
1.22 -
1.23 char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
1.24 uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
1.25
1.26 @@ -88,204 +86,11 @@
1.27 return NULL;
1.28 }
1.29
1.30 -
1.31 -gdrom_disc_t gdrom_image_new( FILE *file )
1.32 -{
1.33 - struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
1.34 - if( disc == NULL )
1.35 - return NULL;
1.36 - disc->read_sectors = gdrom_image_read_sectors;
1.37 - disc->close = gdrom_image_destroy;
1.38 - disc->disc_type = IDE_DISC_CDROM;
1.39 - disc->file = file;
1.40 - return disc;
1.41 -}
1.42 -
1.43 -static void gdrom_image_destroy( gdrom_disc_t disc )
1.44 -{
1.45 - if( disc->file != NULL ) {
1.46 - fclose(disc->file);
1.47 - disc->file = NULL;
1.48 - }
1.49 - free( disc );
1.50 -}
1.51 -
1.52 -static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
1.53 - uint32_t sector_count, int mode, char *buf,
1.54 - uint32_t *length )
1.55 -{
1.56 - int i, file_offset, read_len;
1.57 - struct gdrom_track *track = NULL;
1.58 -
1.59 - for( i=0; i<disc->track_count; i++ ) {
1.60 - if( disc->track[i].lba <= sector &&
1.61 - (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
1.62 - track = &disc->track[i];
1.63 - break;
1.64 - }
1.65 - }
1.66 - if( track == NULL )
1.67 - return PKT_ERR_BADREAD;
1.68 -
1.69 - file_offset = track->offset + track->sector_size * (sector - track->lba);
1.70 - read_len = track->sector_size * sector_count;
1.71 -
1.72 - switch( mode ) {
1.73 - case GDROM_GD:
1.74 - // Temporarily comment this out - it's wrong, but...
1.75 - // if( track->mode != GDROM_GD )
1.76 - // return PKT_ERR_BADREADMODE;
1.77 - // break;
1.78 - case GDROM_MODE1:
1.79 - case GDROM_MODE2_XA1:
1.80 - switch( track->mode ) {
1.81 - case GDROM_MODE1:
1.82 - case GDROM_MODE2_XA1:
1.83 - fseek( disc->file, file_offset, SEEK_SET );
1.84 - fread( buf, track->sector_size, sector_count, disc->file );
1.85 - break;
1.86 - case GDROM_MODE2:
1.87 - read_len = sector_count * 2048;
1.88 - file_offset += 8; /* skip the subheader */
1.89 - while( sector_count > 0 ) {
1.90 - fseek( disc->file, file_offset, SEEK_SET );
1.91 - fread( buf, 2048, 1, disc->file );
1.92 - file_offset += track->sector_size;
1.93 - buf += 2048;
1.94 - sector_count--;
1.95 - }
1.96 - break;
1.97 - default:
1.98 - return PKT_ERR_BADREADMODE;
1.99 - }
1.100 - break;
1.101 - default:
1.102 - return PKT_ERR_BADREADMODE;
1.103 - }
1.104 -
1.105 - *length = read_len;
1.106 - return PKT_ERR_OK;
1.107 -}
1.108 -
1.109 -uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
1.110 - int mode, char *buf, uint32_t *length )
1.111 -{
1.112 - if( gdrom_disc == NULL )
1.113 - return PKT_ERR_NODISC; /* No media */
1.114 - return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
1.115 -}
1.116 -
1.117 -
1.118 -void gdrom_dump_disc_info( gdrom_disc_t disc ) {
1.119 - int i;
1.120 - int last_session = disc->track[disc->track_count-1].session;
1.121 - gboolean is_bootable = FALSE;
1.122 -
1.123 - INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count,
1.124 - disc->track[disc->track_count-1].session + 1 );
1.125 - if( last_session > 0 ) {
1.126 - /* Boot track is the first data track of the last session, provided that it
1.127 - * cannot be a single-session disc.
1.128 - */
1.129 - int boot_track = -1;
1.130 - for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
1.131 - if( disc->track[i].flags & TRACK_DATA ) {
1.132 - boot_track = i;
1.133 - }
1.134 - }
1.135 - if( boot_track != -1 ) {
1.136 - char boot_sector[2048];
1.137 - uint32_t length = sizeof(boot_sector);
1.138 - if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
1.139 - boot_sector, &length ) == PKT_ERR_OK ) {
1.140 - bootstrap_dump(boot_sector, FALSE);
1.141 - is_bootable = TRUE;
1.142 - }
1.143 - }
1.144 - }
1.145 - if( !is_bootable )
1.146 - WARN( "Disc does not appear to be bootable" );
1.147 -}
1.148 -
1.149 -gdrom_error_t gdrom_get_toc( char *buf )
1.150 -{
1.151 - struct gdrom_toc *toc = (struct gdrom_toc *)buf;
1.152 - int i;
1.153 -
1.154 - if( gdrom_disc == NULL )
1.155 - return PKT_ERR_NODISC;
1.156 -
1.157 - for( i=0; i<gdrom_disc->track_count; i++ ) {
1.158 - toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
1.159 - }
1.160 - toc->first = 0x0100 | gdrom_disc->track[0].flags;
1.161 - toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
1.162 - toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
1.163 - gdrom_disc->track[i-1].flags;
1.164 - for( ;i<99; i++ )
1.165 - toc->track[i] = 0xFFFFFFFF;
1.166 - return PKT_ERR_OK;
1.167 -}
1.168 -
1.169 -gdrom_error_t gdrom_get_info( char *buf, int session )
1.170 -{
1.171 - if( gdrom_disc == NULL )
1.172 - return PKT_ERR_NODISC;
1.173 - struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
1.174 - unsigned int end_of_disc = last_track->lba + last_track->sector_count;
1.175 - int i;
1.176 - buf[0] = 0x01; /* Disc status? */
1.177 - buf[1] = 0;
1.178 -
1.179 - if( session == 0 ) {
1.180 - buf[2] = last_track->session+1; /* last session */
1.181 - buf[3] = (end_of_disc >> 16) & 0xFF;
1.182 - buf[4] = (end_of_disc >> 8) & 0xFF;
1.183 - buf[5] = end_of_disc & 0xFF;
1.184 - return PKT_ERR_OK;
1.185 - } else {
1.186 - session--;
1.187 - for( i=0; i<gdrom_disc->track_count; i++ ) {
1.188 - if( gdrom_disc->track[i].session == session ) {
1.189 - buf[2] = i+1; /* first track of session */
1.190 - buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
1.191 - buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
1.192 - buf[5] = gdrom_disc->track[i].lba & 0xFF;
1.193 - return PKT_ERR_OK;
1.194 - }
1.195 - }
1.196 - return PKT_ERR_BADFIELD; /* No such session */
1.197 - }
1.198 -
1.199 -}
1.200 -
1.201 -gdrom_track_t gdrom_get_track( int trackno ) {
1.202 - if( gdrom_disc == NULL || trackno < 1 || trackno > 99 ) {
1.203 - return NULL;
1.204 - } else {
1.205 - return &gdrom_disc->track[trackno-1];
1.206 - }
1.207 -}
1.208 -
1.209 -uint8_t gdrom_get_track_no_by_lba( uint32_t lba ) {
1.210 - int i;
1.211 - if( gdrom_disc != NULL ) {
1.212 - for( i=0; i<gdrom_disc->track_count; i++ ) {
1.213 - if( gdrom_disc->track[i].lba <= lba &&
1.214 - lba <= (gdrom_disc->track[i].lba + gdrom_disc->track[i].sector_count) ) {
1.215 - return i+1;
1.216 - }
1.217 - }
1.218 - }
1.219 - return -1;
1.220 -}
1.221 -
1.222 void gdrom_mount_disc( gdrom_disc_t disc )
1.223 {
1.224 gdrom_unmount_disc();
1.225 gdrom_disc = disc;
1.226 - idereg.disc = disc->disc_type | IDE_DISC_READY;
1.227 - gdrom_dump_disc_info( disc );
1.228 + gdrom_image_dump_info( disc );
1.229 }
1.230
1.231 gdrom_disc_t gdrom_mount_image( const gchar *filename )
1.232 @@ -302,10 +107,6 @@
1.233 gdrom_disc->close(gdrom_disc);
1.234 }
1.235 gdrom_disc = NULL;
1.236 - idereg.disc = IDE_DISC_NONE;
1.237 +
1.238 }
1.239
1.240 -gboolean gdrom_is_mounted( void )
1.241 -{
1.242 - return gdrom_disc != NULL;
1.243 -}
.