Search
lxdream.org :: lxdream/src/gdrom/cdi.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 168:203a72138e16
prev31:495e480360d7
next236:581a9f8bf6a6
author nkeynes
date Mon Jun 26 10:30:42 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Implement multi-format CD image support
Fix CDI support up to (more or less) work
file annotate diff log raw
1.1 --- a/src/gdrom/cdi.c Sun Dec 25 08:24:11 2005 +0000
1.2 +++ b/src/gdrom/cdi.c Mon Jun 26 10:30:42 2006 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: cdi.c,v 1.2 2005-12-25 08:24:11 nkeynes Exp $
1.6 + * $Id: cdi.c,v 1.3 2006-06-26 10:30:42 nkeynes Exp $
1.7 *
1.8 * CDI CD-image file support
1.9 *
1.10 @@ -22,10 +22,18 @@
1.11 #include <fcntl.h>
1.12 #include <errno.h>
1.13 #include <sys/stat.h>
1.14 -#include "cdi.h"
1.15 +#include "gdrom/gdrom.h"
1.16
1.17 -#define CDI_V2 0x80000004
1.18 -#define CDI_V3 0x80000005
1.19 +#define CDI_V2_ID 0x80000004
1.20 +#define CDI_V3_ID 0x80000005
1.21 +#define CDI_V35_ID 0x80000006
1.22 +
1.23 +
1.24 +static gboolean cdi_image_is_valid( FILE *f );
1.25 +static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
1.26 +
1.27 +struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi",
1.28 + cdi_image_is_valid, cdi_image_open };
1.29
1.30 static char track_start_marker[20] = { 0,0,1,0,0,0,255,255,255,255,
1.31 0,0,1,0,0,0,255,255,255,255 };
1.32 @@ -35,24 +43,6 @@
1.33 uint32_t header_offset;
1.34 };
1.35
1.36 -struct cdi_track {
1.37 - int file_posn;
1.38 - int length; /* bytes */
1.39 - int pregap; /* sectors */
1.40 - int mode;
1.41 - int sector_size;
1.42 - int session;
1.43 - struct cdi_track *next;
1.44 -};
1.45 -
1.46 -struct cdi_handle {
1.47 - int fd;
1.48 - uint16_t num_sessions;
1.49 - struct cdi_track *tracks;
1.50 -};
1.51 -
1.52 -
1.53 -
1.54 struct cdi_track_data {
1.55 char unknown[0x19];
1.56 uint32_t pregap_length;
1.57 @@ -67,105 +57,131 @@
1.58 char unknown5[0x1D];
1.59 } __attribute__((packed));
1.60
1.61 -cdi_t cdi_open( char *filename )
1.62 +gboolean cdi_image_is_valid( FILE *f )
1.63 {
1.64 -#define BAIL( x, ... ) { fprintf( stderr, x, __VA_ARGS__ ); if( fd != -1 ) close(fd); return NULL; }
1.65 + int len;
1.66 + struct cdi_trailer trail;
1.67
1.68 - struct stat st;
1.69 + fseek( f, -8, SEEK_END );
1.70 + len = ftell(f)+8;
1.71 + fread( &trail, sizeof(trail), 1, f );
1.72 + if( trail.header_offset >= len ||
1.73 + trail.header_offset == 0 )
1.74 + return FALSE;
1.75 + return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID;
1.76 +}
1.77 +
1.78 +gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
1.79 +{
1.80 + gdrom_disc_t disc = NULL;
1.81 int fd = -1, i,j, tmp;
1.82 + uint16_t session_count;
1.83 + uint16_t track_count;
1.84 + int total_tracks = 0;
1.85 int posn = 0, hdr;
1.86 long len;
1.87 struct cdi_trailer trail;
1.88 - struct cdi_handle cdi;
1.89 - uint16_t tracks;
1.90 uint32_t new_fmt;
1.91 char tmpc;
1.92 char marker[20];
1.93
1.94 - fd = open( filename, O_RDONLY );
1.95 - if( fd == -1 )
1.96 - BAIL( "Unable to open file: %s (%s)\n", filename, strerror(errno) );
1.97 - fstat( fd, &st );
1.98 - if( st.st_size < 8 )
1.99 - BAIL( "File is too small to be a valid CDI image: %s\n", filename );
1.100 - len = lseek( fd, -8, SEEK_END );
1.101 - read( fd, &trail, sizeof(trail) );
1.102 - if( trail.header_offset > st.st_size ||
1.103 + fseek( f, -8, SEEK_END );
1.104 + len = ftell(f)+8;
1.105 + fread( &trail, sizeof(trail), 1, f );
1.106 + if( trail.header_offset >= len ||
1.107 trail.header_offset == 0 )
1.108 - BAIL( "Not a valid CDI image: %s\n", filename );
1.109 + return NULL;
1.110
1.111 - if( trail.cdi_version == CDI_V2 ) trail.cdi_version = 2;
1.112 - else if( trail.cdi_version == CDI_V3 ) trail.cdi_version = 3;
1.113 - else BAIL( "Unknown CDI version code %08x in %s\n", trail.cdi_version,
1.114 - filename );
1.115 + if( trail.cdi_version == CDI_V2_ID ) trail.cdi_version = 2;
1.116 + else if( trail.cdi_version == CDI_V3_ID ) trail.cdi_version = 3;
1.117 + else return NULL;
1.118
1.119 - lseek( fd, trail.header_offset, SEEK_SET );
1.120 - read( fd, &cdi.num_sessions, 2 );
1.121 + fseek( f, trail.header_offset, SEEK_SET );
1.122 + fread( &session_count, sizeof(session_count), 1, f );
1.123
1.124 + disc = gdrom_image_new(f);
1.125
1.126 - printf( "CDI version: %d\n", trail.cdi_version );
1.127 - printf( "Sessions: %d\n\n", cdi.num_sessions );
1.128 - for( i=0; i< cdi.num_sessions; i++ ) {
1.129 - read( fd, &tracks, 2 );
1.130 - printf( "Session %d - %d tracks:\n", i+1, tracks );
1.131 - for( j=0; j<tracks; j++ ) {
1.132 + for( i=0; i< session_count; i++ ) {
1.133 + fread( &track_count, sizeof(track_count), 1, f );
1.134 + for( j=0; j<track_count; j++ ) {
1.135 struct cdi_track_data trk;
1.136 -
1.137 - read( fd, &new_fmt, 4 );
1.138 + uint32_t new_fmt = 0;
1.139 + uint8_t fnamelen = 0;
1.140 + fread( &new_fmt, sizeof(new_fmt), 1, f );
1.141 if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
1.142 - printf( "Note: CDI image has 3.00.780+ flag set\n" );
1.143 - lseek( fd, 8, SEEK_CUR );
1.144 + fseek( f, 8, SEEK_CUR ); /* Skip */
1.145 }
1.146 - read( fd, marker, 20 );
1.147 - if( memcmp( marker, track_start_marker, 20) != 0 )
1.148 - BAIL( "Track start marker not found, error reading cdi\n",NULL );
1.149 - read( fd, &tmp, 4 );
1.150 - printf( "Unknown 4 bytes: %08x\n", tmp );
1.151 - read( fd, &tmpc, 1 );
1.152 - lseek( fd, (int)tmpc, SEEK_CUR ); /* skip over the filename */
1.153 - read( fd, &trk, sizeof(trk) );
1.154 - switch( trk.sector_size ) {
1.155 - case 0: trk.sector_size = 2048; hdr=0; break;
1.156 - case 1: trk.sector_size = 2336; hdr=8; break;
1.157 - case 2:
1.158 - trk.sector_size = 2352;
1.159 - if( trk.mode == 2 ) hdr=24;
1.160 - else hdr=16;
1.161 - break;
1.162 - default: BAIL( "Unknown sector size: %d\n", trk.sector_size );
1.163 - }
1.164 - posn += hdr;
1.165 - len = trk.length*trk.sector_size;
1.166 - printf( " Track %d\n", j+1 );
1.167 - printf( " Pregap: %08x\n", trk.pregap_length );
1.168 - printf( " Length: %08x\n", trk.length );
1.169 - printf( " Mode: %d\n", trk.mode );
1.170 - printf( " Sector size: %d\n", trk.sector_size );
1.171 - printf( " Start LBA: %08x\n", trk.start_lba );
1.172 - printf( " Total length: %08x\n", trk.total_length );
1.173 - printf( " ---\n" );
1.174 - printf( " File position: %08x\n", posn+trk.pregap_length*trk.sector_size );
1.175 - printf( " File length: %d\n", len );
1.176 - posn += trk.total_length*trk.sector_size;
1.177 + fread( marker, 20, 1, f );
1.178 + if( memcmp( marker, track_start_marker, 20) != 0 ) {
1.179 + ERROR( "Track start marker not found, error reading cdi image\n" );
1.180 + free(disc);
1.181 + return NULL;
1.182 + }
1.183 + fseek( f, 4, SEEK_CUR );
1.184 + fread( &fnamelen, 1, 1, f );
1.185 + fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
1.186 + fread( &trk, sizeof(trk), 1, f );
1.187 + disc->track[total_tracks].session = i;
1.188 + disc->track[total_tracks].lba = trk.start_lba;
1.189 + disc->track[total_tracks].sector_count = trk.length;
1.190 + switch( trk.mode ) {
1.191 + case 0:
1.192 + disc->track[total_tracks].mode = GDROM_CDDA;
1.193 + disc->track[total_tracks].sector_size = 2352;
1.194 + disc->track[total_tracks].flags = 0x01;
1.195 + if( trk.sector_size != 2 ) {
1.196 + ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
1.197 + free(disc);
1.198 + return NULL;
1.199 + }
1.200 + break;
1.201 + case 1:
1.202 + disc->track[total_tracks].mode = GDROM_MODE1;
1.203 + disc->track[total_tracks].sector_size = 2048;
1.204 + disc->track[total_tracks].flags = 0x41;
1.205 + if( trk.sector_size != 0 ) {
1.206 + ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
1.207 + free(disc);
1.208 + return NULL;
1.209 + }
1.210 + break;
1.211 + case 2:
1.212 + disc->track[total_tracks].flags = 0x41;
1.213 + switch( trk.sector_size ) {
1.214 + case 0:
1.215 + disc->track[total_tracks].mode = GDROM_MODE2_XA1;
1.216 + disc->track[total_tracks].sector_size = 2048;
1.217 + break;
1.218 + case 1:
1.219 + disc->track[total_tracks].mode = GDROM_MODE2;
1.220 + disc->track[total_tracks].sector_size = 2336;
1.221 + break;
1.222 + case 2:
1.223 + default:
1.224 + ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
1.225 + free(disc);
1.226 + return NULL;
1.227 + }
1.228 + break;
1.229 + default:
1.230 + ERROR( "Unsupported track mode %d", trk.mode );
1.231 + free(disc);
1.232 + return NULL;
1.233 + }
1.234 + disc->track[total_tracks].offset = posn +
1.235 + trk.pregap_length * disc->track[total_tracks].sector_size ;
1.236 + posn += trk.total_length * disc->track[total_tracks].sector_size;
1.237 + total_tracks++;
1.238 lseek( fd, 12, SEEK_CUR );
1.239 - if( new_fmt )
1.240 - lseek( fd, 78, SEEK_CUR );
1.241 - tmp = lseek( fd, 0, SEEK_CUR );
1.242 - printf( "(Header offset: %x)\n", tmp - trail.header_offset );
1.243 - }
1.244 + if( new_fmt ) {
1.245 + fseek( f, 90, SEEK_CUR );
1.246 + } else {
1.247 + fseek( f, 12, SEEK_CUR );
1.248 + }
1.249 + }
1.250 }
1.251 -
1.252 - return NULL;
1.253 -#undef BAIL(x, ...)
1.254 + disc->track_count = total_tracks;
1.255 + disc->file = f;
1.256 + disc->filename = filename;
1.257 + return disc;
1.258 }
1.259 -
1.260 -int main(int argc, char *argv[] )
1.261 -{
1.262 - int i;
1.263 -
1.264 - for( i=1; i<argc; i++ ) {
1.265 - cdi_open(argv[i]);
1.266 - }
1.267 - return 0;
1.268 -}
1.269 -
.