Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 347:e686be8b10df
prev342:850502f0e8de
next422:61a0598e07ff
author nkeynes
date Wed Sep 19 12:09:33 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Add a 'translate and run' method for running uncached code
view annotate diff log raw
     1 /**
     2  * $Id: cdi.c,v 1.6 2007-02-04 11:30:41 nkeynes Exp $
     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 <fcntl.h>
    23 #include <errno.h>
    24 #include <sys/stat.h>
    25 #include "gdrom/gdrom.h"
    27 #define CDI_V2_ID 0x80000004
    28 #define CDI_V3_ID 0x80000005
    29 #define CDI_V35_ID 0x80000006
    32 static gboolean cdi_image_is_valid( FILE *f );
    33 static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
    35 struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi", 
    36 					     cdi_image_is_valid, cdi_image_open };
    38 static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
    39                                        0,0,1,0,0,0,255,255,255,255 };
    40 static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
    42 struct cdi_trailer {
    43     uint32_t cdi_version;
    44     uint32_t header_offset;
    45 };
    47 struct cdi_track_data {
    48     char unknown[0x19];
    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 }
    75 gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
    76 {
    77     gdrom_disc_t disc = NULL;
    78     gdrom_image_t image;
    79     int fd = -1, i,j, tmp;
    80     uint16_t session_count;
    81     uint16_t track_count;
    82     int total_tracks = 0;
    83     int posn = 0, hdr;
    84     long len;
    85     struct cdi_trailer trail;
    86     uint32_t new_fmt;
    87     char tmpc;
    88     char marker[20];
    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 NULL;
    97     if( trail.cdi_version == CDI_V2_ID ) trail.cdi_version = 2;
    98     else if( trail.cdi_version == CDI_V3_ID ) trail.cdi_version = 3;
    99     else return NULL; 
   101     fseek( f, trail.header_offset, SEEK_SET );
   102     fread( &session_count, sizeof(session_count), 1, f );
   104     disc = gdrom_image_new(f);
   105     if( disc == NULL ) {
   106 	fclose(f);
   107 	ERROR("Unable to allocate memory!");
   108 	return NULL;
   109     }
   110     image = (gdrom_image_t)disc;
   112     for( i=0; i< session_count; i++ ) {        
   113 	fread( &track_count, sizeof(track_count), 1, f );
   114 	if( track_count + total_tracks > 99 ) {
   115 	    ERROR( "Invalid number of tracks, bad cdi image\n" );
   116 	    disc->close(disc);
   117 	    return NULL;
   118 	}
   119         for( j=0; j<track_count; j++ ) {
   120             struct cdi_track_data trk;
   121             uint32_t new_fmt = 0;
   122 	    uint8_t fnamelen = 0;
   123             fread( &new_fmt, sizeof(new_fmt), 1, f );
   124             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   125 		fseek( f, 8, SEEK_CUR ); /* Skip */
   126             }
   127             fread( marker, 20, 1, f );
   128             if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
   129 		ERROR( "Track start marker not found, error reading cdi image\n" );
   130 		disc->close(disc);
   131 		return NULL;
   132 	    }
   133 	    fseek( f, 4, SEEK_CUR );
   134             fread( &fnamelen, 1, 1, f );
   135             fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
   136             fread( &trk, sizeof(trk), 1, f );
   137 	    image->track[total_tracks].session = i;
   138 	    image->track[total_tracks].lba = trk.start_lba + 150;
   139 	    image->track[total_tracks].sector_count = trk.length;
   140 	    switch( trk.mode ) {
   141 	    case 0:
   142 		image->track[total_tracks].mode = GDROM_CDDA;
   143 		image->track[total_tracks].sector_size = 2352;
   144 		image->track[total_tracks].flags = 0x01;
   145 		if( trk.sector_size != 2 ) {
   146 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   147 		    disc->close(disc);
   148 		    return NULL;
   149 		}
   150 		break;
   151 	    case 1:
   152 		image->track[total_tracks].mode = GDROM_MODE1;
   153 		image->track[total_tracks].sector_size = 2048;
   154 		image->track[total_tracks].flags = 0x41;
   155 		if( trk.sector_size != 0 ) {
   156 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   157 		    disc->close(disc);
   158 		    return NULL;
   159 		}
   160 		break;
   161 	    case 2:
   162 		image->track[total_tracks].flags = 0x41;
   163 		switch( trk.sector_size ) {
   164 		case 0:
   165 		    image->track[total_tracks].mode = GDROM_MODE2_XA1;
   166 		    image->track[total_tracks].sector_size = 2048;
   167 		    break;
   168 		case 1:
   169 		    image->track[total_tracks].mode = GDROM_MODE2;
   170 		    image->track[total_tracks].sector_size = 2336;
   171 		    break;
   172 		case 2:
   173 		default:
   174 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   175 		    disc->close(disc);
   176 		    return NULL;
   177 		}
   178 		break;
   179 	    default:
   180 		ERROR( "Unsupported track mode %d", trk.mode );
   181 		disc->close(disc);
   182 		return NULL;
   183 	    }
   184 	    image->track[total_tracks].offset = posn + 
   185 		trk.pregap_length * image->track[total_tracks].sector_size ;
   186 	    posn += trk.total_length * image->track[total_tracks].sector_size;
   187 	    total_tracks++;
   188 	    fread( marker, 1, 9, f );
   189 	    if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
   190 		fseek( f, 91, SEEK_CUR );
   191 	    } else {
   192 		fseek( f, 3, SEEK_CUR );
   193 	    }
   194 	}
   195     }
   196     image->track_count = total_tracks;
   197     image->filename = filename;
   198     return disc;
   199 }
.