4 * CDI CD-image file support
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.
26 #include "gdrom/gddriver.h"
28 #define CDI_V2_ID 0x80000004
29 #define CDI_V3_ID 0x80000005
30 #define CDI_V35_ID 0x80000006
33 static gboolean cdi_image_is_valid( FILE *f );
34 static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
36 struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi",
37 cdi_image_is_valid, cdi_image_open };
39 static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
40 0,0,1,0,0,0,255,255,255,255 };
41 static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
45 uint32_t header_offset;
48 struct cdi_track_data {
49 uint32_t pregap_length;
55 uint32_t total_length;
59 } __attribute__((packed));
61 gboolean cdi_image_is_valid( FILE *f )
64 struct cdi_trailer trail;
66 fseek( f, -8, SEEK_END );
68 fread( &trail, sizeof(trail), 1, f );
69 if( trail.header_offset >= len ||
70 trail.header_offset == 0 )
72 return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID ||
73 trail.cdi_version == CDI_V35_ID;
76 gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
78 gdrom_disc_t disc = NULL;
81 uint16_t session_count;
86 struct cdi_trailer trail;
89 fseek( f, -8, SEEK_END );
91 fread( &trail, sizeof(trail), 1, f );
92 if( trail.header_offset >= len ||
93 trail.header_offset == 0 )
96 if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
97 trail.cdi_version != CDI_V35_ID ) {
101 if( trail.cdi_version == CDI_V35_ID ) {
102 fseek( f, -trail.header_offset, SEEK_END );
104 fseek( f, trail.header_offset, SEEK_SET );
106 fread( &session_count, sizeof(session_count), 1, f );
108 disc = gdrom_image_new(filename, f);
110 ERROR("Unable to allocate memory!");
113 image = (gdrom_image_t)disc;
115 for( i=0; i< session_count; i++ ) {
116 fread( &track_count, sizeof(track_count), 1, f );
117 if( track_count + total_tracks > 99 ) {
118 ERROR( "Invalid number of tracks, bad cdi image\n" );
119 gdrom_image_destroy_no_close(disc);
122 for( j=0; j<track_count; j++ ) {
123 struct cdi_track_data trk;
124 uint32_t new_fmt = 0;
125 uint8_t fnamelen = 0;
126 fread( &new_fmt, sizeof(new_fmt), 1, f );
127 if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
128 fseek( f, 8, SEEK_CUR ); /* Skip */
130 fread( marker, 20, 1, f );
131 if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
132 ERROR( "Track start marker not found, error reading cdi image\n" );
133 gdrom_image_destroy_no_close(disc);
136 fseek( f, 4, SEEK_CUR );
137 fread( &fnamelen, 1, 1, f );
138 fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
139 fseek( f, 19, SEEK_CUR );
140 fread( &new_fmt, sizeof(new_fmt), 1, f );
141 if( new_fmt == 0x80000000 ) {
142 fseek( f, 10, SEEK_CUR );
144 fseek( f, 2, SEEK_CUR );
146 fread( &trk, sizeof(trk), 1, f );
147 image->track[total_tracks].session = i;
148 image->track[total_tracks].lba = trk.start_lba + 150;
149 image->track[total_tracks].sector_count = trk.length;
152 image->track[total_tracks].mode = GDROM_CDDA;
153 image->track[total_tracks].sector_size = 2352;
154 image->track[total_tracks].flags = 0x01;
155 if( trk.sector_size != 2 ) {
156 ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
157 gdrom_image_destroy_no_close(disc);
162 image->track[total_tracks].mode = GDROM_MODE1;
163 image->track[total_tracks].sector_size = 2048;
164 image->track[total_tracks].flags = 0x41;
165 if( trk.sector_size != 0 ) {
166 ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
167 gdrom_image_destroy_no_close(disc);
172 image->track[total_tracks].flags = 0x41;
173 switch( trk.sector_size ) {
175 image->track[total_tracks].mode = GDROM_MODE2_FORM1;
176 image->track[total_tracks].sector_size = 2048;
179 image->track[total_tracks].mode = GDROM_SEMIRAW_MODE2;
180 image->track[total_tracks].sector_size = 2336;
184 ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
185 gdrom_image_destroy_no_close(disc);
190 ERROR( "Unsupported track mode %d", trk.mode );
191 gdrom_image_destroy_no_close(disc);
194 image->track[total_tracks].offset = posn +
195 trk.pregap_length * image->track[total_tracks].sector_size ;
196 posn += trk.total_length * image->track[total_tracks].sector_size;
198 fread( marker, 1, 9, f );
199 if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
200 fseek( f, 79, SEEK_CUR );
202 fseek( f, -9, SEEK_CUR );
205 fseek( f, 12, SEEK_CUR );
207 image->track_count = total_tracks;
.