2 * $Id: cdi.c,v 1.2 2005-12-25 08:24:11 nkeynes Exp $
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.
27 #define CDI_V2 0x80000004
28 #define CDI_V3 0x80000005
30 static char track_start_marker[20] = { 0,0,1,0,0,0,255,255,255,255,
31 0,0,1,0,0,0,255,255,255,255 };
35 uint32_t header_offset;
40 int length; /* bytes */
41 int pregap; /* sectors */
45 struct cdi_track *next;
50 uint16_t num_sessions;
51 struct cdi_track *tracks;
56 struct cdi_track_data {
58 uint32_t pregap_length;
64 uint32_t total_length;
68 } __attribute__((packed));
70 cdi_t cdi_open( char *filename )
72 #define BAIL( x, ... ) { fprintf( stderr, x, __VA_ARGS__ ); if( fd != -1 ) close(fd); return NULL; }
75 int fd = -1, i,j, tmp;
78 struct cdi_trailer trail;
79 struct cdi_handle cdi;
85 fd = open( filename, O_RDONLY );
87 BAIL( "Unable to open file: %s (%s)\n", filename, strerror(errno) );
90 BAIL( "File is too small to be a valid CDI image: %s\n", filename );
91 len = lseek( fd, -8, SEEK_END );
92 read( fd, &trail, sizeof(trail) );
93 if( trail.header_offset > st.st_size ||
94 trail.header_offset == 0 )
95 BAIL( "Not a valid CDI image: %s\n", filename );
97 if( trail.cdi_version == CDI_V2 ) trail.cdi_version = 2;
98 else if( trail.cdi_version == CDI_V3 ) trail.cdi_version = 3;
99 else BAIL( "Unknown CDI version code %08x in %s\n", trail.cdi_version,
102 lseek( fd, trail.header_offset, SEEK_SET );
103 read( fd, &cdi.num_sessions, 2 );
106 printf( "CDI version: %d\n", trail.cdi_version );
107 printf( "Sessions: %d\n\n", cdi.num_sessions );
108 for( i=0; i< cdi.num_sessions; i++ ) {
109 read( fd, &tracks, 2 );
110 printf( "Session %d - %d tracks:\n", i+1, tracks );
111 for( j=0; j<tracks; j++ ) {
112 struct cdi_track_data trk;
114 read( fd, &new_fmt, 4 );
115 if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
116 printf( "Note: CDI image has 3.00.780+ flag set\n" );
117 lseek( fd, 8, SEEK_CUR );
119 read( fd, marker, 20 );
120 if( memcmp( marker, track_start_marker, 20) != 0 )
121 BAIL( "Track start marker not found, error reading cdi\n",NULL );
123 printf( "Unknown 4 bytes: %08x\n", tmp );
124 read( fd, &tmpc, 1 );
125 lseek( fd, (int)tmpc, SEEK_CUR ); /* skip over the filename */
126 read( fd, &trk, sizeof(trk) );
127 switch( trk.sector_size ) {
128 case 0: trk.sector_size = 2048; hdr=0; break;
129 case 1: trk.sector_size = 2336; hdr=8; break;
131 trk.sector_size = 2352;
132 if( trk.mode == 2 ) hdr=24;
135 default: BAIL( "Unknown sector size: %d\n", trk.sector_size );
138 len = trk.length*trk.sector_size;
139 printf( " Track %d\n", j+1 );
140 printf( " Pregap: %08x\n", trk.pregap_length );
141 printf( " Length: %08x\n", trk.length );
142 printf( " Mode: %d\n", trk.mode );
143 printf( " Sector size: %d\n", trk.sector_size );
144 printf( " Start LBA: %08x\n", trk.start_lba );
145 printf( " Total length: %08x\n", trk.total_length );
147 printf( " File position: %08x\n", posn+trk.pregap_length*trk.sector_size );
148 printf( " File length: %d\n", len );
149 posn += trk.total_length*trk.sector_size;
150 lseek( fd, 12, SEEK_CUR );
152 lseek( fd, 78, SEEK_CUR );
153 tmp = lseek( fd, 0, SEEK_CUR );
154 printf( "(Header offset: %x)\n", tmp - trail.header_offset );
162 int main(int argc, char *argv[] )
166 for( i=1; i<argc; i++ ) {
.