filename | src/drivers/cdrom/cd_cdi.c |
changeset | 1109:700c5ab26a63 |
prev | 1097:d4807997e450 |
next | 1180:f2b9f4ef1987 |
author | nkeynes |
date | Sun Oct 24 15:22:59 2010 +1000 (13 years ago) |
permissions | -rw-r--r-- |
last change | Eliminate GL_REPLACE tex mode in favour of GL_MODULATE (by setting colour values to 1.0) - one less case for shaders to care about later |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * CDI CD-image file support
5 *
6 * Copyright (c) 2005 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/stat.h>
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 };
43 struct cdi_trailer {
44 uint32_t cdi_version;
45 uint32_t header_offset;
46 };
48 struct cdi_track_data {
49 uint32_t pregap_length;
50 uint32_t length;
51 char unknown2[6];
52 uint32_t mode;
53 char unknown3[0x0c];
54 uint32_t start_lba;
55 uint32_t total_length;
56 char unknown4[0x10];
57 uint32_t sector_size;
58 char unknown5[0x1D];
59 } __attribute__((packed));
61 gboolean cdi_image_is_valid( FILE *f )
62 {
63 int len;
64 struct cdi_trailer trail;
66 fseek( f, -8, SEEK_END );
67 len = ftell(f)+8;
68 fread( &trail, sizeof(trail), 1, f );
69 if( trail.header_offset >= len ||
70 trail.header_offset == 0 )
71 return FALSE;
72 return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID ||
73 trail.cdi_version == CDI_V35_ID;
74 }
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 )
79 {
80 int i,j;
81 uint16_t session_count;
82 uint16_t track_count;
83 int total_tracks = 0;
84 int posn = 0;
85 long len;
86 struct cdi_trailer trail;
87 char marker[20];
89 FILE *f = cdrom_disc_get_base_file(disc);
90 fseek( f, -8, SEEK_END );
91 len = ftell(f)+8;
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" );
96 }
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" );
101 }
103 if( trail.cdi_version == CDI_V35_ID ) {
104 fseek( f, -(long)trail.header_offset, SEEK_END );
105 } else {
106 fseek( f, trail.header_offset, SEEK_SET );
107 }
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( track_count + total_tracks > 99 ) {
113 RETURN_PARSE_ERROR("Invalid number of tracks, bad cdi image" );
114 }
115 for( j=0; j<track_count; j++ ) {
116 struct cdi_track_data trk;
117 uint32_t new_fmt = 0;
118 uint8_t fnamelen = 0;
119 fread( &new_fmt, sizeof(new_fmt), 1, f );
120 if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
121 fseek( f, 8, SEEK_CUR ); /* Skip */
122 }
123 fread( marker, 20, 1, f );
124 if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
125 RETURN_PARSE_ERROR( "Track start marker not found, error reading cdi image" );
126 }
127 fseek( f, 4, SEEK_CUR );
128 fread( &fnamelen, 1, 1, f );
129 fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
130 fseek( f, 19, SEEK_CUR );
131 fread( &new_fmt, sizeof(new_fmt), 1, f );
132 if( new_fmt == 0x80000000 ) {
133 fseek( f, 10, SEEK_CUR );
134 } else {
135 fseek( f, 2, SEEK_CUR );
136 }
137 fread( &trk, sizeof(trk), 1, f );
138 disc->track[total_tracks].sessionno = i+1;
139 disc->track[total_tracks].lba = trk.start_lba;
140 cdrom_count_t sector_count = trk.length;
141 sector_mode_t mode;
142 switch( trk.mode ) {
143 case 0:
144 mode = SECTOR_CDDA;
145 disc->track[total_tracks].flags = 0x01;
146 if( trk.sector_size != 2 ) {
147 RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
148 }
149 break;
150 case 1:
151 mode = SECTOR_MODE1;
152 disc->track[total_tracks].flags = 0x41;
153 if( trk.sector_size != 0 ) {
154 RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
155 }
156 break;
157 case 2:
158 disc->track[total_tracks].flags = 0x41;
159 switch( trk.sector_size ) {
160 case 0:
161 mode = SECTOR_MODE2_FORM1;
162 break;
163 case 1:
164 mode = SECTOR_SEMIRAW_MODE2;
165 break;
166 case 2:
167 default:
168 RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
169 }
170 break;
171 default:
172 RETURN_PARSE_ERROR( "Unsupported track mode %d", trk.mode );
173 }
174 uint32_t offset = posn +
175 trk.pregap_length * CDROM_SECTOR_SIZE(mode);
176 disc->track[total_tracks].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
177 posn += trk.total_length * CDROM_SECTOR_SIZE(mode);
178 total_tracks++;
179 fread( marker, 1, 9, f );
180 if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
181 fseek( f, 79, SEEK_CUR );
182 } else {
183 fseek( f, -9, SEEK_CUR );
184 }
185 }
186 fseek( f, 12, SEEK_CUR );
187 }
189 disc->track_count = total_tracks;
190 disc->session_count = session_count;
191 return TRUE;
192 }
.