Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 513:ae1dea71613d
prev468:3a49695e081a
next561:533f6b478071
author nkeynes
date Thu Nov 22 11:10:15 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Re-add "Load Binary" menu item (misplaced in GUI rewrite)
Prevent running with no code loaded
view annotate diff log raw
     1 /**
     2  * $Id: cdi.c,v 1.10 2007-10-28 07:23:46 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     uint32_t pregap_length;
    49     uint32_t length;
    50     char unknown2[6];
    51     uint32_t mode;
    52     char unknown3[0x0c];
    53     uint32_t start_lba;
    54     uint32_t total_length;
    55     char unknown4[0x10];
    56     uint32_t sector_size;
    57     char unknown5[0x1D];
    58 } __attribute__((packed));
    60 gboolean cdi_image_is_valid( FILE *f )
    61 {
    62     int len;
    63     struct cdi_trailer trail;
    65     fseek( f, -8, SEEK_END );
    66     len = ftell(f)+8;
    67     fread( &trail, sizeof(trail), 1, f );
    68     if( trail.header_offset >= len ||
    69         trail.header_offset == 0 )
    70 	return FALSE;
    71     return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID ||
    72 	trail.cdi_version == CDI_V35_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 i,j;
    80     uint16_t session_count;
    81     uint16_t track_count;
    82     int total_tracks = 0;
    83     int posn = 0;
    84     long len;
    85     struct cdi_trailer trail;
    86     char marker[20];
    88     fseek( f, -8, SEEK_END );
    89     len = ftell(f)+8;
    90     fread( &trail, sizeof(trail), 1, f );
    91     if( trail.header_offset >= len ||
    92         trail.header_offset == 0 )
    93 	return NULL;
    95     if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
    96 	trail.cdi_version != CDI_V35_ID ) {
    97 	return NULL;
    98     }
   100     if( trail.cdi_version == CDI_V35_ID ) {
   101 	fseek( f, -trail.header_offset, SEEK_END );
   102     } else {
   103 	fseek( f, trail.header_offset, SEEK_SET );
   104     }
   105     fread( &session_count, sizeof(session_count), 1, f );
   107     disc = gdrom_image_new(filename, f);
   108     if( disc == NULL ) {
   109 	ERROR("Unable to allocate memory!");
   110 	return NULL;
   111     }
   112     image = (gdrom_image_t)disc;
   114     for( i=0; i< session_count; i++ ) {        
   115 	fread( &track_count, sizeof(track_count), 1, f );
   116 	if( track_count + total_tracks > 99 ) {
   117 	    ERROR( "Invalid number of tracks, bad cdi image\n" );
   118 	    gdrom_image_destroy_no_close(disc);
   119 	    return NULL;
   120 	}
   121         for( j=0; j<track_count; j++ ) {
   122             struct cdi_track_data trk;
   123             uint32_t new_fmt = 0;
   124 	    uint8_t fnamelen = 0;
   125             fread( &new_fmt, sizeof(new_fmt), 1, f );
   126             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   127 		fseek( f, 8, SEEK_CUR ); /* Skip */
   128             }
   129             fread( marker, 20, 1, f );
   130             if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
   131 		ERROR( "Track start marker not found, error reading cdi image\n" );
   132 		gdrom_image_destroy_no_close(disc);
   133 		return NULL;
   134 	    }
   135 	    fseek( f, 4, SEEK_CUR );
   136             fread( &fnamelen, 1, 1, f );
   137             fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
   138 	    fseek( f, 19, SEEK_CUR );
   139 	    fread( &new_fmt, sizeof(new_fmt), 1, f );
   140 	    if( new_fmt == 0x80000000 ) {
   141 		fseek( f, 10, SEEK_CUR );
   142 	    } else {
   143 		fseek( f, 2, SEEK_CUR );
   144 	    }
   145             fread( &trk, sizeof(trk), 1, f );
   146 	    image->track[total_tracks].session = i;
   147 	    image->track[total_tracks].lba = trk.start_lba + 150;
   148 	    image->track[total_tracks].sector_count = trk.length;
   149 	    switch( trk.mode ) {
   150 	    case 0:
   151 		image->track[total_tracks].mode = GDROM_CDDA;
   152 		image->track[total_tracks].sector_size = 2352;
   153 		image->track[total_tracks].flags = 0x01;
   154 		if( trk.sector_size != 2 ) {
   155 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   156 		    gdrom_image_destroy_no_close(disc);
   157 		    return NULL;
   158 		}
   159 		break;
   160 	    case 1:
   161 		image->track[total_tracks].mode = GDROM_MODE1;
   162 		image->track[total_tracks].sector_size = 2048;
   163 		image->track[total_tracks].flags = 0x41;
   164 		if( trk.sector_size != 0 ) {
   165 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   166 		    gdrom_image_destroy_no_close(disc);
   167 		    return NULL;
   168 		}
   169 		break;
   170 	    case 2:
   171 		image->track[total_tracks].flags = 0x41;
   172 		switch( trk.sector_size ) {
   173 		case 0:
   174 		    image->track[total_tracks].mode = GDROM_MODE2_XA1;
   175 		    image->track[total_tracks].sector_size = 2048;
   176 		    break;
   177 		case 1:
   178 		    image->track[total_tracks].mode = GDROM_MODE2;
   179 		    image->track[total_tracks].sector_size = 2336;
   180 		    break;
   181 		case 2:
   182 		default:
   183 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   184 		    gdrom_image_destroy_no_close(disc);
   185 		    return NULL;
   186 		}
   187 		break;
   188 	    default:
   189 		ERROR( "Unsupported track mode %d", trk.mode );
   190 		gdrom_image_destroy_no_close(disc);
   191 		return NULL;
   192 	    }
   193 	    image->track[total_tracks].offset = posn + 
   194 		trk.pregap_length * image->track[total_tracks].sector_size ;
   195 	    posn += trk.total_length * image->track[total_tracks].sector_size;
   196 	    total_tracks++;
   197 	    fread( marker, 1, 9, f );
   198 	    if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
   199 		fseek( f, 79, SEEK_CUR );
   200 	    } else {
   201 		fseek( f, -9, SEEK_CUR );
   202 	    }
   203 	}
   204 	fseek( f, 12, SEEK_CUR );
   205     }
   206     image->track_count = total_tracks;
   207     return disc;
   208 }
.