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 "drivers/cdrom/cdimpl.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 gboolean cdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
36 struct cdrom_disc_factory cdi_disc_factory = { "DiscJuggler", "cdi",
37 cdi_image_is_valid, NULL, cdi_image_read_toc };
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 #define RETURN_PARSE_ERROR( ... ) do { SET_ERROR(err, LX_ERR_FILE_INVALID, __VA_ARGS__); return FALSE; } while(0)
78 static gboolean cdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
81 uint16_t session_count;
86 struct cdi_trailer trail;
89 FILE *f = cdrom_disc_get_base_file(disc);
90 fseek( f, -8, SEEK_END );
92 fread( &trail, sizeof(trail), 1, f );
93 if( trail.header_offset >= len ||
94 trail.header_offset == 0 ) {
95 RETURN_PARSE_ERROR( "Invalid CDI image" );
98 if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
99 trail.cdi_version != CDI_V35_ID ) {
100 RETURN_PARSE_ERROR( "Invalid CDI image" );
103 if( trail.cdi_version == CDI_V35_ID ) {
104 fseek( f, -(long)trail.header_offset, SEEK_END );
106 fseek( f, trail.header_offset, SEEK_SET );
108 fread( &session_count, sizeof(session_count), 1, f );
110 for( i=0; i< session_count; i++ ) {
111 fread( &track_count, sizeof(track_count), 1, f );
112 if( (i != session_count-1 && track_count < 1) || track_count > 99 ) {
113 RETURN_PARSE_ERROR("Invalid number of tracks (%d), bad cdi image", track_count);
115 if( track_count + total_tracks > 99 ) {
116 RETURN_PARSE_ERROR("Invalid number of tracks in disc, bad cdi image" );
118 for( j=0; j<track_count; j++ ) {
119 struct cdi_track_data trk;
120 uint32_t new_fmt = 0;
121 uint8_t fnamelen = 0;
122 fread( &new_fmt, sizeof(new_fmt), 1, f );
123 if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
124 fseek( f, 8, SEEK_CUR ); /* Skip */
126 fread( marker, 20, 1, f );
127 if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
128 RETURN_PARSE_ERROR( "Track start marker not found, error reading cdi image" );
130 fseek( f, 4, SEEK_CUR );
131 fread( &fnamelen, 1, 1, f );
132 fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
133 fseek( f, 19, SEEK_CUR );
134 fread( &new_fmt, sizeof(new_fmt), 1, f );
135 if( new_fmt == 0x80000000 ) {
136 fseek( f, 10, SEEK_CUR );
138 fseek( f, 2, SEEK_CUR );
140 fread( &trk, sizeof(trk), 1, f );
141 disc->track[total_tracks].sessionno = i+1;
142 disc->track[total_tracks].lba = trk.start_lba;
143 cdrom_count_t sector_count = trk.length;
148 disc->track[total_tracks].flags = 0x01;
149 if( trk.sector_size != 2 ) {
150 RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
155 disc->track[total_tracks].flags = 0x41;
156 if( trk.sector_size != 0 ) {
157 RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
161 disc->track[total_tracks].flags = 0x41;
162 switch( trk.sector_size ) {
164 mode = SECTOR_MODE2_FORM1;
167 mode = SECTOR_SEMIRAW_MODE2;
171 RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
175 RETURN_PARSE_ERROR( "Unsupported track mode %d", trk.mode );
177 uint32_t offset = posn +
178 trk.pregap_length * CDROM_SECTOR_SIZE(mode);
179 disc->track[total_tracks].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
180 posn += trk.total_length * CDROM_SECTOR_SIZE(mode);
182 if( trail.cdi_version != CDI_V2_ID ) {
184 fseek( f, 5, SEEK_CUR );
185 fread( &extmarker, sizeof(extmarker), 1, f);
186 if( extmarker == 0xFFFFFFFF ) {
187 fseek( f, 78, SEEK_CUR );
191 fseek( f, 12, SEEK_CUR );
192 if( trail.cdi_version != CDI_V2_ID ) {
193 fseek( f, 1, SEEK_CUR );
197 disc->track_count = total_tracks;
198 disc->session_count = session_count;
.