Search
lxdream.org :: lxdream :: r168:203a72138e16
lxdream 0.9.1
released Jun 29
Download Now
changeset168:203a72138e16
parent167:71c0cc416a64
child169:abbdc6943587
authornkeynes
dateMon Jun 26 10:30:42 2006 +0000 (14 years ago)
Implement multi-format CD image support
Fix CDI support up to (more or less) work
src/gdrom/cdi.c
src/gdrom/gdrom.c
src/gdrom/gdrom.h
src/gdrom/nrg.c
1.1 --- a/src/gdrom/cdi.c Mon Jun 19 11:00:42 2006 +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 -
2.1 --- a/src/gdrom/gdrom.c Mon Jun 19 11:00:42 2006 +0000
2.2 +++ b/src/gdrom/gdrom.c Mon Jun 26 10:30:42 2006 +0000
2.3 @@ -1,5 +1,5 @@
2.4 /**
2.5 - * $Id: gdrom.c,v 1.8 2006-06-19 11:00:42 nkeynes Exp $
2.6 + * $Id: gdrom.c,v 1.9 2006-06-26 10:30:42 nkeynes Exp $
2.7 *
2.8 * GD-Rom access functions.
2.9 *
2.10 @@ -17,7 +17,7 @@
2.11 */
2.12
2.13 #include <stdio.h>
2.14 -
2.15 +#include <errno.h>
2.16 #include "gdrom/ide.h"
2.17 #include "gdrom/gdrom.h"
2.18 #include "gdrom/packet.h"
2.19 @@ -26,6 +26,7 @@
2.20 static void gdrom_image_destroy( gdrom_disc_t );
2.21 static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, int, char *, uint32_t * );
2.22
2.23 +gdrom_image_class_t gdrom_image_classes[] = { &nrg_image_class, &cdi_image_class, NULL };
2.24
2.25 gdrom_disc_t gdrom_disc = NULL;
2.26
2.27 @@ -34,7 +35,45 @@
2.28
2.29 gdrom_disc_t gdrom_image_open( const gchar *filename )
2.30 {
2.31 - return nrg_image_open( filename );
2.32 + const gchar *ext = strrchr(filename, '.');
2.33 + gdrom_disc_t disc = NULL;
2.34 + FILE *f = fopen(filename, "ro");
2.35 + int i,j;
2.36 + gdrom_image_class_t extclz = NULL;
2.37 +
2.38 + if( f == NULL ) {
2.39 + ERROR("Unable to open file '%s': %s", filename, strerror(errno));
2.40 + return NULL;
2.41 + }
2.42 +
2.43 + /* try extensions */
2.44 + if( ext != NULL ) {
2.45 + ext++; /* Skip the '.' */
2.46 + for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
2.47 + if( strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
2.48 + extclz = gdrom_image_classes[i];
2.49 + if( extclz->is_valid_file(f) ) {
2.50 + disc = extclz->open_image_file(filename, f);
2.51 + if( disc != NULL )
2.52 + return disc;
2.53 + }
2.54 + break;
2.55 + }
2.56 + }
2.57 + }
2.58 +
2.59 + /* Okay, fall back to magic */
2.60 + for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
2.61 + if( gdrom_image_classes[i] != extclz &&
2.62 + gdrom_image_classes[i]->is_valid_file(f) ) {
2.63 + disc = gdrom_image_classes[i]->open_image_file(filename, f);
2.64 + if( disc != NULL )
2.65 + return disc;
2.66 + }
2.67 + }
2.68 +
2.69 + fclose(f);
2.70 + return NULL;
2.71 }
2.72
2.73
3.1 --- a/src/gdrom/gdrom.h Mon Jun 19 11:00:42 2006 +0000
3.2 +++ b/src/gdrom/gdrom.h Mon Jun 26 10:30:42 2006 +0000
3.3 @@ -1,5 +1,5 @@
3.4 /**
3.5 - * $Id: gdrom.h,v 1.6 2006-05-23 13:11:45 nkeynes Exp $
3.6 + * $Id: gdrom.h,v 1.7 2006-06-26 10:30:42 nkeynes Exp $
3.7 *
3.8 * This file defines the structures and functions used by the GD-Rom
3.9 * disc driver. (ie, the modules that supply a CD image to be used by the
3.10 @@ -84,6 +84,19 @@
3.11 } *gdrom_disc_t;
3.12
3.13 /**
3.14 + *
3.15 + */
3.16 +typedef struct gdrom_image_class {
3.17 + const gchar *name;
3.18 + const gchar *extension;
3.19 + gboolean (*is_valid_file)(FILE *f);
3.20 + gdrom_disc_t (*open_image_file)(const gchar *filename, FILE *f);
3.21 +} *gdrom_image_class_t;
3.22 +
3.23 +extern struct gdrom_image_class nrg_image_class;
3.24 +extern struct gdrom_image_class cdi_image_class;
3.25 +
3.26 +/**
3.27 * Construct a new image file using the default methods.
3.28 */
3.29 gdrom_disc_t gdrom_image_new( FILE *file );
3.30 @@ -92,7 +105,6 @@
3.31 * Open an image file
3.32 */
3.33 gdrom_disc_t gdrom_image_open( const gchar *filename );
3.34 -gdrom_disc_t nrg_image_open( const gchar *filename );
3.35
3.36 /**
3.37 * Retrieve the disc table of contents, and write it into the buffer in the
4.1 --- a/src/gdrom/nrg.c Mon Jun 19 11:00:42 2006 +0000
4.2 +++ b/src/gdrom/nrg.c Mon Jun 26 10:30:42 2006 +0000
4.3 @@ -1,5 +1,5 @@
4.4 /**
4.5 - * $Id: nrg.c,v 1.2 2006-05-02 14:09:11 nkeynes Exp $
4.6 + * $Id: nrg.c,v 1.3 2006-06-26 10:30:42 nkeynes Exp $
4.7 *
4.8 * Nero (NRG) CD file format. File information stolen shamelessly from
4.9 * libcdio.
4.10 @@ -23,6 +23,12 @@
4.11 #include "gdrom/gdrom.h"
4.12 #include "dream.h"
4.13
4.14 +static gboolean nrg_image_is_valid( FILE *f );
4.15 +static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f );
4.16 +
4.17 +struct gdrom_image_class nrg_image_class = { "Nero", "nrg",
4.18 + nrg_image_is_valid, nrg_image_open };
4.19 +
4.20 #define NERO_V55_ID 0x4e455235
4.21 #define NERO_V50_ID 0x4e45524f
4.22
4.23 @@ -131,9 +137,21 @@
4.24 }
4.25 }
4.26
4.27 -gdrom_disc_t nrg_image_open( const gchar *filename )
4.28 +static gboolean nrg_image_is_valid( FILE *f )
4.29 {
4.30 - FILE *f = fopen( filename, "ro" );
4.31 + union nrg_footer footer;
4.32 +
4.33 + fseek( f, -12, SEEK_END );
4.34 + fread( &footer, sizeof(footer), 1, f );
4.35 + if( ntohl(footer.v50.id) == NERO_V50_ID ) {
4.36 + return TRUE;
4.37 + } else {
4.38 + return FALSE;
4.39 + }
4.40 +}
4.41 +
4.42 +static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f )
4.43 +{
4.44 union nrg_footer footer;
4.45 struct nrg_chunk chunk;
4.46 struct nrg_daoi *dao;
4.47 @@ -145,11 +163,6 @@
4.48 int cue_track_id = 0, cue_track_count = 0;
4.49 int i;
4.50
4.51 - if( f == NULL ) {
4.52 - ERROR( "Unable to open file '%s': %s", filename, strerror(errno) );
4.53 - return NULL;
4.54 - }
4.55 -
4.56 fseek( f, -12, SEEK_END );
4.57 fread( &footer, sizeof(footer), 1, f );
4.58 if( ntohl(footer.v50.id) == NERO_V50_ID ) {
4.59 @@ -159,14 +172,13 @@
4.60 INFO( "Loading Nero 5.5+ image" );
4.61 fseek( f, ntohl(footer.v55.offset), SEEK_SET );
4.62 } else {
4.63 - ERROR("Unable to understand file '%s' as a Nero image", filename );
4.64 + /* Not a (recognized) Nero image */
4.65 return NULL;
4.66 }
4.67
4.68 disc = gdrom_image_new(f);
4.69 if( disc == NULL ) {
4.70 ERROR("Unable to allocate memory!");
4.71 - fclose(f);
4.72 return NULL;
4.73 }
4.74
.