Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 342:850502f0e8de
prev236:581a9f8bf6a6
next347:e686be8b10df
author nkeynes
date Wed Jan 31 10:58:42 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Refactor gdrom module to be more conducive to real device support
view annotate diff log raw
     1 /**
     2  * $Id: cdi.c,v 1.5 2007-01-31 10:58:42 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 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 };
    41 struct cdi_trailer {
    42     uint32_t cdi_version;
    43     uint32_t header_offset;
    44 };
    46 struct cdi_track_data {
    47     char unknown[0x19];
    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 }
    74 gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
    75 {
    76     gdrom_disc_t disc = NULL;
    77     gdrom_image_t image;
    78     int fd = -1, i,j, tmp;
    79     uint16_t session_count;
    80     uint16_t track_count;
    81     int total_tracks = 0;
    82     int posn = 0, hdr;
    83     long len;
    84     struct cdi_trailer trail;
    85     uint32_t new_fmt;
    86     char tmpc;
    87     char marker[20];
    89     fseek( f, -8, SEEK_END );
    90     len = ftell(f)+8;
    91     fread( &trail, sizeof(trail), 1, f );
    92     if( trail.header_offset >= len ||
    93         trail.header_offset == 0 )
    94 	return NULL;
    96     if( trail.cdi_version == CDI_V2_ID ) trail.cdi_version = 2;
    97     else if( trail.cdi_version == CDI_V3_ID ) trail.cdi_version = 3;
    98     else return NULL; 
   100     fseek( f, trail.header_offset, SEEK_SET );
   101     fread( &session_count, sizeof(session_count), 1, f );
   103     disc = gdrom_image_new(f);
   104     if( disc == NULL ) {
   105 	fclose(f);
   106 	ERROR("Unable to allocate memory!");
   107 	return NULL;
   108     }
   109     image = (gdrom_image_t)disc;
   111     for( i=0; i< session_count; i++ ) {        
   112 	fread( &track_count, sizeof(track_count), 1, f );
   113         for( j=0; j<track_count; j++ ) {
   114             struct cdi_track_data trk;
   115             uint32_t new_fmt = 0;
   116 	    uint8_t fnamelen = 0;
   117             fread( &new_fmt, sizeof(new_fmt), 1, f );
   118             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   119 		fseek( f, 8, SEEK_CUR ); /* Skip */
   120             }
   121             fread( marker, 20, 1, f );
   122             if( memcmp( marker, track_start_marker, 20) != 0 ) {
   123 		ERROR( "Track start marker not found, error reading cdi image\n" );
   124 		disc->close(disc);
   125 		return NULL;
   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             fread( &trk, sizeof(trk), 1, f );
   131 	    image->track[total_tracks].session = i;
   132 	    image->track[total_tracks].lba = trk.start_lba + 150;
   133 	    image->track[total_tracks].sector_count = trk.length;
   134 	    switch( trk.mode ) {
   135 	    case 0:
   136 		image->track[total_tracks].mode = GDROM_CDDA;
   137 		image->track[total_tracks].sector_size = 2352;
   138 		image->track[total_tracks].flags = 0x01;
   139 		if( trk.sector_size != 2 ) {
   140 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   141 		    disc->close(disc);
   142 		    return NULL;
   143 		}
   144 		break;
   145 	    case 1:
   146 		image->track[total_tracks].mode = GDROM_MODE1;
   147 		image->track[total_tracks].sector_size = 2048;
   148 		image->track[total_tracks].flags = 0x41;
   149 		if( trk.sector_size != 0 ) {
   150 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   151 		    disc->close(disc);
   152 		    return NULL;
   153 		}
   154 		break;
   155 	    case 2:
   156 		image->track[total_tracks].flags = 0x41;
   157 		switch( trk.sector_size ) {
   158 		case 0:
   159 		    image->track[total_tracks].mode = GDROM_MODE2_XA1;
   160 		    image->track[total_tracks].sector_size = 2048;
   161 		    break;
   162 		case 1:
   163 		    image->track[total_tracks].mode = GDROM_MODE2;
   164 		    image->track[total_tracks].sector_size = 2336;
   165 		    break;
   166 		case 2:
   167 		default:
   168 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   169 		    disc->close(disc);
   170 		    return NULL;
   171 		}
   172 		break;
   173 	    default:
   174 		ERROR( "Unsupported track mode %d", trk.mode );
   175 		disc->close(disc);
   176 		return NULL;
   177 	    }
   178 	    image->track[total_tracks].offset = posn + 
   179 		trk.pregap_length * image->track[total_tracks].sector_size ;
   180 	    posn += trk.total_length * image->track[total_tracks].sector_size;
   181 	    total_tracks++;
   182             lseek( fd, 12, SEEK_CUR );
   183             if( new_fmt ) {
   184                 fseek( f, 90, SEEK_CUR );
   185 	    } else {
   186 		fseek( f, 12, SEEK_CUR );
   187 	    }
   188 	}
   189     }
   190     image->track_count = total_tracks;
   191     image->filename = filename;
   192     return disc;
   193 }
.